#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//EllipseShape.cpp  - Ellipse Object found in Image      
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Aug 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#include "EllipseShape.h"

#include "ImageDataset.h"

const char * const EllipseShape::xmlNotes = "Notes";
const char * const EllipseShape::xmlTopLeftx = "TopLeftx";
const char * const EllipseShape::xmlTopLefty = "TopLefty";
const char * const EllipseShape::xmlBotRightx = "BotRightx";
const char * const EllipseShape::xmlBotRighty = "BotRighty";

EllipseShape::EllipseShape(void)
	: ImageObject('\0')
{
	SetNote( ".");
	_pImageBoundingRect = new ImageRect(); 
}

EllipseShape::EllipseShape(ImageObjectType *pObjType)
	: ImageObject(pObjType)
{
	SetNote( ".");
	_pImageBoundingRect = new ImageRect(); 
}


EllipseShape::~EllipseShape(void)
{
	if (_pImageBoundingRect != '\0')
		delete _pImageBoundingRect;
}


bool EllipseShape::LoadProperties(TiXmlElement *pImageObjectXml)
{
	bool rc = false;

	if ((pImageObjectXml != '\0') && (_pImageBoundingRect != '\0'))
	{
		std::string name = pImageObjectXml->GetText();
		SetName(name.c_str());

		SetNote(ImageDataset::GetXmlAttribute(pImageObjectXml, EllipseShape::xmlNotes, "..."));

		if ( LoadXmlDefinedProperties(pImageObjectXml))
		{
			int topLeftx = 0;
			if ( pImageObjectXml->Attribute( EllipseShape::xmlTopLeftx, &topLeftx) != '\0')
			{
				int topLefty = 0;
				if ( pImageObjectXml->Attribute( EllipseShape::xmlTopLefty, &topLefty) != '\0')
				{
					int botRightx = 0;
					if ( pImageObjectXml->Attribute( EllipseShape::xmlBotRightx, &botRightx) != '\0')
					{
						int botRighty = 0;
						if ( pImageObjectXml->Attribute( EllipseShape::xmlBotRighty, &botRighty) != '\0')
						{
							_pImageBoundingRect->SetTopLeft(topLeftx, topLefty);
							_pImageBoundingRect->SetBotRight(botRightx, botRighty);
							rc = true;
						}
					}
				}
			}
		}
	}

	return rc;
}

bool EllipseShape::SaveProperties(TiXmlElement *pImageObjectXml)
{
	bool rc = true;

	if ((pImageObjectXml) != '\0' && (_pImageBoundingRect != '\0'))
	{
		std::string imageObjectTypeName = pImageObjectXml->Attribute("Type");
		if ( imageObjectTypeName ==  GetImageObjectType()->GetName())
		{
			//Notes
			ImageDataset::SetXmlAttribute(pImageObjectXml, PtShape::xmlNotes, GetNote());
	
			if ( SaveXmlDefinedProperties(pImageObjectXml))
			{
				ImagePt topLeft = _pImageBoundingRect->GetTopLeft();
				ImagePt botRight = _pImageBoundingRect->GetBotRight();

				pImageObjectXml->SetAttribute(EllipseShape::xmlTopLeftx, topLeft.x);
				pImageObjectXml->SetAttribute(EllipseShape::xmlTopLefty, topLeft.y);
				pImageObjectXml->SetAttribute(EllipseShape::xmlBotRightx, botRight.x);
				pImageObjectXml->SetAttribute(EllipseShape::xmlBotRighty, botRight.y);
		
				rc = true;
			}
		}
	}
	return rc;
}


bool EllipseShape::AssignFrom(ImageObject *pObjectToCopy)
{
	bool rc = false;	
	EllipseShape *pSrc = dynamic_cast<EllipseShape *>(pObjectToCopy);

	if(pSrc != '\0' )
	{
		SetImageObjectType(pSrc->GetImageObjectType()); 
		_pImageBoundingRect = pSrc->GetBoundary();
		SetNote(pSrc->GetNote());

		rc = true;
	}
	return rc;
}


//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------